import   nprogress  from "nprogress"
// 引入小仓库
import {useInfo} from "@/store/data"
// 引入大仓库库
import pinia from "@/store"
// 引入路由器
import  {router} from "./index"
// 注册实例对象
const $useInfo=useInfo(pinia)

router.beforeEach( async (to,from,next)=>{
    console.log(to);
    let token=$useInfo.token;
let  username =$useInfo.username
    nprogress.start();
    if(token){
       if(to.path==="/login"){
         next({path:"/"})
       }
       else{
        if(username){
            next()
        }
        else{
            
            try{
                console.log("123");
                await $useInfo.userinfo()
                next()

            }
            
            
            catch(err){
                $useInfo.token=""
                 $useInfo.username=""
                localStorage.removeItem("token");
                next({path:"/login"})

            }

             
        }






       }
    }
    else{
        if(to.path==="/login"){
            next()
        }
        else{
            next({path:"/login"})
        }
       
    }
})
router.afterEach((to,from)=>{
    nprogress.done();

})

